package cn.com.dhcc.app.pub.module.usermgr.dao;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.base.BaseDao;
import cn.com.dhcc.app.core.base.CmCommon;
import cn.com.dhcc.app.core.base.log.LogType;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.component.db.orm.valuebean.LikeValue;
import cn.com.dhcc.app.core.component.log.Log;
import cn.com.dhcc.app.core.component.pager.Pager;
import cn.com.dhcc.app.core.component.pager.PagerData;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.tag.CommonStaticCode.ENTITY_STATUS;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.pub.core.Table;
import cn.com.dhcc.app.pub.module.usermgr.vo.UserInfoVo;

@Repository
public class CmUserMgrDao extends BaseDao {

	Logger logger = Log.getLogger(LogType.BUSILOG);

	public WhereCondition parseUserInfo(UserInfoVo vo) throws DaoException {
		Method[] methods = vo.getClass().getDeclaredMethods();
		WhereCondition searchParameter = new WhereCondition();
		searchParameter.where1Eq1();
		
		for (Method method : methods) {
			String methodName = method.getName();
			if(methodName.startsWith("get")&&method.getReturnType().equals(String.class)){
				String subName = methodName.substring(3, methodName.length());
				String setName = "set" + subName;
				try {
					String value = (String) method.invoke(vo, new Object[]{});
					if(StrUtil.isBlank(value)){
						Method setMethod = vo.getClass().getMethod(setName, String.class);
						setMethod.invoke(vo, "");
					} else {
						Method setMethod = vo.getClass().getMethod(setName, String.class);
						setMethod.invoke(vo, value.trim());
						searchParameter.andLike(subName, LikeValue.roundLike(value.trim()));
					}
				} catch (IllegalArgumentException e) {
					logger.error("处理CasUser中null出错", e);
					throw new DaoException(e.getMessage(), e);
				} catch (IllegalAccessException e) {
					logger.error("处理CasUser中null出错", e);
					throw new DaoException(e.getMessage(), e);
				} catch (SecurityException e) {
					logger.error("处理CasUser中null出错", e);
					throw new DaoException(e.getMessage(), e);
				} catch (NoSuchMethodException e) {
					logger.error("处理CasUser中null出错", e);
					throw new DaoException(e.getMessage(), e);
				} catch (InvocationTargetException e) {
					logger.error("处理CasUser中null出错", e);
					throw new DaoException(e.getMessage(), e);
				}
			}
		}
		return searchParameter;
		
	}
	
	public PagerData<UserInfoVo> listUser(String string, Pager pager,
			UserInfoVo userInfoVo, String order, String orderName) throws DaoException {
		String msg = "获取用户列表";
		String preWhereSql = "select * FROM "+WSD_TABLE(Table.SYS_BASE_TABLE.SYS_BASE_USER);
		WhereCondition searchParameter = parseUserInfo(userInfoVo);
		searchParameter.andNotEq("status", ENTITY_STATUS.DELETE.getCode());
		searchParameter.orderBy(orderName+" "+order);
		try {
			return CmCommon.getDao().getPagerModuleList("获取用户列表", preWhereSql, UserInfoVo.class, new Object[]{} , searchParameter,pager);
		} catch (DaoException e) {
			msg = msg + "失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
}
